import { Autocomplete } from '@aws-amplify/ui-react'; import { ComponentClassTable } from '@/components/ComponentClassTable'; import { ComponentStyleDisplay } from '@/components/ComponentStyleDisplay'; import { Example, ExampleCode } from '@/components/Example'; import { Fragment } from '@/components/Fragment'; import StandardHTMLAttributes from '@/components/StandardHTMLAttributes.mdx'; import ThemeExample from '@/components/ThemeExample.mdx'; import { AutocompleteDemo } from './demo'; import { AutocompleteControlledExample, AutocompleteCustomEmptyExample, AutocompleteCustomFilteringExample, AutocompleteCustomFooterExample, AutocompleteCustomHeaderExample, AutocompleteCustomLoadingExample, AutocompleteCustomOptionExample, AutocompleteLoadingStateExample, AutocompletePlaceholderExample, AutocompleteSizeExample, AutocompleteVariationExample, AutocompleteStylePropsExample, AutocompleteThemeExample, DefaultAutocompleteExample, } from './examples'; ## Demo ## Usage Import the `Autocomplete`, provide `options` for autocomplete and a `label` for accessibility/usability. ```tsx file=./examples/DefaultAutocompleteExample.tsx ``` ### Accessibility / Label behavior {() => import('./../shared/formFieldAccessibility.mdx')} ### Controlled component ```tsx file=./examples/AutocompleteControlledExample.tsx ``` ***Note***: When using Autocomplete in controlled way, you are also responsible to set up `onClear` and `onSelect` event handlers in addition to `onChange` since the input value is under your control. ### Placeholder Text that appears in `Autocomplete` when it has no value set. ```tsx file=./examples/AutocompletePlaceholderExample.tsx ``` ### Sizes Use the `size` prop to change `Autocomplete` size. Available options are `small`, `large`, and none (default). ```tsx file=./examples/AutocompleteSizeExample.tsx ``` ### Variations There are two variation styles available: default and `quiet`. ```tsx file=./examples/AutocompleteVariationExample.tsx ``` ### Loading state By setting `isLoading` to `true`, `Autocomplete` will be in loading state. ```tsx file=./examples/AutocompleteLoadingStateExample.tsx ``` ### Custom filtering By default, `Autocomplete` will filter against option `label`. You can customize the filtering behavior by providing `filteringOptions`. ```tsx file=./examples/AutocompleteCustomFilteringExample.tsx ``` ***Note***: When you create your own filtering logic, match highlighting will be disabled. If you want to enable it, you can build a custom option and wrap the `label` with `HighlightMatch`. See [custom option example](#custom-option). ### Custom option You can create a custom option by providing `renderOption`. ```tsx file=./examples/AutocompleteCustomOptionExample.tsx ``` ### Custom menu You can customize the option menu by setting `menu`. The available slots accept a `ReactNode`. #### Custom header ```tsx file=./examples/AutocompleteCustomHeaderExample.tsx ``` #### Custom footer ```tsx file=./examples/AutocompleteCustomFooterExample.tsx ``` #### Custom empty ```tsx file=./examples/AutocompleteCustomEmptyExample.tsx ``` #### Custom loading ```tsx file=./examples/AutocompleteCustomLoadingExample.tsx ``` ## Styling ```tsx file=./examples/AutocompleteThemeExample.tsx ``` ### Target classes ### Global styling To override styling on all Autocompletes, you can set the Amplify CSS variables with the built-in `.amplify-autocomplete` class. ```css /* styles.css */ .amplify-autocomplete { --amplify-components-autocomplete-menu-option-active-background-color: var(--amplify-colors-brand-secondary-80); } ``` ### Local styling To override styling on a specific Autocomplete, you can use (in order of increasing specificity): a class selector and style props. _Using a class selector:_ ```css /* styles.css */ .my-autocomplete { --amplify-components-autocomplete-menu-option-active-background-color: var(--amplify-colors-brand-secondary-80); } ``` _Using style props:_ ```tsx file=./examples/AutocompleteStylePropsExample.tsx ```